{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# ContextGem: Aspect Extraction with Justifications"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# ContextGem: Aspect Extraction with Justifications\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import Aspect, Document, DocumentLLM\n",
        "\n",
        "\n",
        "# Create a document instance\n",
        "doc = Document(\n",
        "    raw_text=(\n",
        "        \"NON-DISCLOSURE AGREEMENT\\n\"\n",
        "        \"\\n\"\n",
        "        'This Non-Disclosure Agreement (\"Agreement\") is entered into between TechCorp Inc. '\n",
        "        '(\"Disclosing Party\") and Innovation Labs LLC (\"Receiving Party\") on January 15, 2024.\\n'\n",
        "        \"...\\n\"\n",
        "    ),\n",
        ")\n",
        "\n",
        "# Define a single aspect focused on NDA direction with justifications\n",
        "nda_direction_aspect = Aspect(\n",
        "    name=\"NDA Direction\",\n",
        "    description=\"Provisions informing the NDA direction (whether mutual or one-way) and information flow between parties\",\n",
        "    add_justifications=True,\n",
        "    justification_depth=\"balanced\",\n",
        "    justification_max_sents=4,\n",
        ")\n",
        "\n",
        "# Add the aspect to the document\n",
        "doc.aspects = [nda_direction_aspect]\n",
        "\n",
        "# Configure DocumentLLM with your API parameters\n",
        "llm = DocumentLLM(\n",
        "    model=\"azure/gpt-4.1-mini\",\n",
        "    api_key=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_KEY\"),\n",
        "    api_version=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_VERSION\"),\n",
        "    api_base=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_BASE\"),\n",
        ")\n",
        "\n",
        "# Extract the aspect with justifications\n",
        "nda_direction_aspect = llm.extract_aspects_from_document(doc)[0]\n",
        "for i, item in enumerate(nda_direction_aspect.extracted_items, 1):\n",
        "    print(f\"- {i}. {item.value}\")\n",
        "    print(f\"  Justification: {item.justification}\")\n",
        "    print()\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}